/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { beforeAll, describe, expect, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from './common/testCommon';
import path from 'path';
import { Rule } from '../../../src/Index';
import { CheckEntry } from '../../../src/utils/common/CheckEntry';
import { WindowSizeChangeListenerCheck } from '../../../src/checker/cross-device-app-dev/WindowSizeChangeListenerCheck';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    const rule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/WindowSizeChangeListener', rule, CHECK_MODE.PROJECT2CHECK, WindowSizeChangeListenerCheck);
    realPath = checkEntry.scene.getRealProjectDir();
});

describe('WindowSizeChangeListenerCheckTest', () => {
    test('WindowSizeChangeListenerCheckTest_001_Report', () => {
        const detectFile: string = path.join(realPath, 'WindowSizeChangeListenerReport.ts');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(1);
        const report = detectFileReports[0];
        expect(report.defect.description).toContain("Enable listening for window size changes when possible.");
        // 打印实际的行列号用于调试
        console.log(`Report: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        // 验证告警的行列号
        const endCol = Number(report.defect.mergeKey.split('%')[3]);
        expect(report.defect.reportLine).toBe(14); // createWindow 所在行
        expect(report.defect.reportColumn).toBe(14); // createWindow 起始列
        expect(endCol).toBe(26); // createWindow 结束列
    });

    test('WindowSizeChangeListenerCheckTest_002_NoReport', () => {
        const detectFile: string = path.join(realPath, 'WindowSizeChangeListenerNoReport.ts');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });

    test('WindowSizeChangeListenerCheckTest_003_PrivateMemberReport', () => {
        const detectFile: string = path.join(realPath, 'WindowSizeChangeListenerPrivateMemberReport.ts');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(1);
        const report = detectFileReports[0];
        // 打印实际的行列号用于调试
        console.log(`Report: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        // 验证告警的行列号
        const endCol2 = Number(report.defect.mergeKey.split('%')[3]);
        expect(report.defect.reportLine).toBe(14); // createWindow 所在行
        expect(report.defect.reportColumn).toBe(14); // createWindow 起始列
        expect(endCol2).toBe(26); // createWindow 结束列
    });

    test('WindowSizeChangeListenerCheckTest_004_PrivateMemberNoReport', () => {
        const detectFile: string = path.join(realPath, 'WindowSizeChangeListenerPrivateMemberNoReport.ts');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });

    test('WindowSizeChangeListenerCheckTest_005_Report1', () => {
        const detectFile: string = path.join(realPath, 'WindowSizeChangeListenerReport1.ts');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(1);
        const report = detectFileReports[0];
        expect(report.defect.description).toContain("Enable listening for window size changes when possible.");
        // 打印实际的行列号用于调试
        console.log(`Report: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        // 补全行列号校验
        const endCol3 = Number(report.defect.mergeKey.split('%')[3]);
        expect(report.defect.reportLine).toBe(14); // createWindow 所在行
        expect(report.defect.reportColumn).toBe(14); // createWindow 起始列
        expect(endCol3).toBe(26); // createWindow 结束列
    });

    test('WindowSizeChangeListenerCheckTest_006_NoReport1', () => {
        const detectFile: string = path.join(realPath, 'WindowSizeChangeListenerNoReport1.ts');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });
});
